// pages/recordSearch/recordSearch.js
const utils = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start: '',
    end: '',
    dIndex: '',
    name: '',
    department_id: ''
  },
  bindStartChange(e) {
    console.log(e)
    this.setData({
      start: e.detail.value
    })
  },
  formInputChange(e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  bindEndChange(e) {
    console.log(e)
    this.setData({
      end: e.detail.value
    })
  },
  getDepartment() {
    let that = this;
    utils.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`, res=> {
      console.log(res.data)
      that.setData({
        department: res.data
      })
    }, {status: 1})
  },
  departmentChange(e) {
    console.log(e)
    let index = e.detail.value;
    let that = this;
    this.setData({
      department_id: that.data.department[index].id,
      dIndex: index
    })
  },
  submitForm() {
    const that =  this;
    if(that.data.start){
      if(!that.data.end) {
        utils.showToast('请选择结束日期！','none', 2000)
        return
      }
    }
    if(that.data.end){
      if(!that.data.start) {
        utils.showToast('请选择开始日期！','none', 2000)
        return
      }
    }
    let u_id = ''
    if(that.data.name){
      utils.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_id`,res => {
        console.log(res)
        if(res.data) {
          u_id = res.data.id
          wx.redirectTo({
            url: '/pages/record/record?user_id='+ u_id + '&start=' + that.data.start + '&end=' + that.data.end + '&department_id=' + that.data.department_id
          })
        } else {
          utils.showToast('员工不存在！', 'none', 2000)
        }
        
      },{
        name: that.data.name
      })
    } else {
      wx.redirectTo({
        url: '/pages/record/record?user_id='+ u_id + '&start=' + that.data.start + '&end=' + that.data.end + '&department_id=' + that.data.department_id
      })
    }
    
    
  },
  cancle() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.setNavBarTitle('记录检索')
    this.getDepartment()
    this.getTomorrow()
  },
  getTomorrow() {
    const that =this;
    const date = new Date();
    const year = date.getFullYear()
    let month = date.getMonth() + 1;
    let day = date.getDate() + 1;
    month = month>9 ? month : '0' + month;
    day = day>9 ? day : '0' + day;
    const tomorrow =  `${year}-${month}-${day}`
    that.setData({
      tomorrow: tomorrow
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})